package com.ithema.edu.info.manager.dao;

import com.ithema.edu.info.manager.domain.Teacher;

import java.io.*;
import java.util.ArrayList;


public class BaseTeacherDao implements OutherTeacherDao {


    private static ArrayList<Teacher> List = new ArrayList<>();

    static {
        BaseReader();
    }

    public BaseTeacherDao() throws IOException {
    }

    //从文件取出内容

    public static void BaseReader() {
        BufferedReader bur = null;
        try {
            bur = new BufferedReader(new FileReader("E:\\JavaSE\\JavaSE\\AdvancedCode4\\教师管理系统老师端.txt"));
            String b;
            while ((b = bur.readLine()) != null) {
                String[] split = b.split(",");
                List.add(new Teacher(split[0], split[1], Integer.parseInt(split[2]), split[3]));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bur.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    // 存入文件
    public void BaseWrite() {

        BufferedWriter buw = null;
        try {
            buw = new BufferedWriter(new FileWriter("E:\\JavaSE\\JavaSE\\AdvancedCode4\\教师管理系统老师端.txt"));
            for (int i = 0; i < List.size(); i++) {
                Teacher teacher = List.get(i);
                buw.write(teacher.getId() + "," + teacher.getName() + "," + teacher.getAge() + "," + teacher.getPhone());
                buw.newLine();

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                buw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    static {

    }

    public void addTeachers(Teacher teacher) {

        List.add(teacher);

        BaseWrite();

    }

    public void deleteTeachers(int index) {
        List.remove(index);
        BaseWrite();


    }

    public ArrayList<Teacher> queryTeacher() {


        return List;


    }

    public void updateTeacher(int exists, Teacher newteacher) {


        List.set(exists, newteacher);

        BaseWrite();


    }
}
